package com.book.controller;

import com.book.entity.Role;
import com.book.service.LuaService;
import com.book.service.RoleService;
import com.book.util.JsonResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author SuperYu  2024/6/24
 */
@RestController
@Slf4j
public class SaleController {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    LuaService luaService;

    @PutMapping("/sale")
    public JsonResult sale() {

        String num = stringRedisTemplate.opsForValue().get("goods");
        int n = Integer.parseInt(num);
        if(n > 0){
            n--;
            stringRedisTemplate.opsForValue().set("goods",n +"" );
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String leftOver = stringRedisTemplate.opsForValue().get("goods");
            log.info("商品出售数量：1 , 还剩下 {} " , leftOver);
        }else {

        }
        return JsonResult.ok(200);
    }

    @PutMapping("/sale-lua")
    public JsonResult saleLua() {

        long num = luaService.executeLua();
        if(num == 0){
            log.info("商品出售完毕 , 还剩下 {}", num);
        }else {
            log.info("商品出售数量：1 , 还剩下 {} " , num);
        }
        return JsonResult.ok(200);
    }
}


